import preutils from '@/common/preutils.js'
import title1 from '../template/title/templateTitle1.js'
import order1 from '../template/order/templateOrder1.js'
export default {
	printTitle(e){
		var titlePrint = uni.getStorageSync(preutils.titlePrint);
		if(titlePrint){
			this.printSomething(titlePrint,e,1);
		}
	}, 
	printOrder(e){
		var orderPrint = uni.getStorageSync(preutils.orderPrint);
		if(orderPrint){
			this.printSomething(orderPrint,e,2); 
		}
	}, 
	printSomething: function(dev,e,t) {
		var that = this;
		var main = plus.android.runtimeMainActivity();
		var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
		var UUID = plus.android.importClass("java.util.UUID");
		var uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
		var BAdapter = BluetoothAdapter.getDefaultAdapter();
		var device = BAdapter.getRemoteDevice(dev.address);
		plus.android.importClass(device);
		var bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
		plus.android.importClass(bluetoothSocket);
		console.log("开始连接打印机:" + dev.name);
		if (!bluetoothSocket.isConnected()) {
			bluetoothSocket.connect();
			if (bluetoothSocket.isConnected()) {
				console.log("设备已连接,开始发送打印文件");
				var outputStream = bluetoothSocket.getOutputStream();
				plus.android.importClass(outputStream);
				console.log(outputStream)
				that.printSwitch(outputStream,e,t);
				bluetoothSocket.close();
				if (!bluetoothSocket.isConnected()) {
					console.log("设备已关闭");
				}
			} else {
				uni.showToast({
					title: '设备连接失败',
					icon: 'error',
					duration: 2000
				});
			}
		}
	},
	
	printSwitch(outputStream,e,t){
		if(t == 1){//标签
			title1.printData(outputStream,e);
		}
		if(t == 2){//托运单
			order1.printTitle(outputStream,e);
		}
		
	}
}